package cnpm02.j2ee.group3.model;

import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.PERSIST;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.NotNull;

@Entity
@Table(name = "FLIGHT_LINE", uniqueConstraints = @UniqueConstraint(columnNames = {
                "FromCityId", "ToCityId"}))
@AttributeOverride(name = "id", column = @Column(name = "FlightLineId"))
public class FlightLine extends AbstractEntity {
	private static final long serialVersionUID = 1L;
	
	@ManyToOne(cascade = {PERSIST, MERGE})
	@JoinColumn(name = "FromCityId", nullable = false)
	@NotNull
	private City from;
	
	@ManyToOne(cascade = {PERSIST, MERGE})
	@JoinColumn(name = "ToCityId", nullable = false)
	@NotNull
	private City to;
	
	@OneToOne(cascade = {PERSIST, MERGE}, fetch = FetchType.LAZY)
	@JoinColumn(name = "ReturnFlightLine", nullable = true, unique = true)
	//@NotNull
	private FlightLine returnFlightLine;
	
	public void setFrom(City from) {
		this.from = from;
	}
	
	public City getFrom() {
		return from;
	}
	
	public void setTo(City to) {
		this.to = to;
	}
	
	public City getTo() {
		return to;
	}

	public void setReturnFlight(FlightLine returnFlight) {
		this.returnFlightLine = returnFlight;
	}

	public FlightLine getReturnFlight() {
		return returnFlightLine;
	}
	
	@Override
	public String toString() {
		return from.getCode() + " - " + to.getCode();
	}
	
	public FlightLine()
	{
		super();
	}
	
	public FlightLine(City _from, City _to, FlightLine _returnFlightLine)
	{
		this();
		from = _from;
		to = _to;
		returnFlightLine = _returnFlightLine;
	}
}
